package com.us.usws.batch.resimp;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

import net.iwsh.core.common.GlobalConfig;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileListVideoWalker extends SimpleFileVisitor<Path> {

	private static Log log = LogFactory.getFactory().getInstance(FileListVideoWalker.class);
	public static final String DESC_FILE = GlobalConfig.getString("BatchImport/DescFileName", "list.csv");
	private List<ResDescFile> result = new ArrayList<>();

	private void find(Path path) {
		if (!Files.isDirectory(path)) {
			//	log.info("[find]: fileName=[" + path.getFileName() + "], parent=[" + path.getParent().toString() + "], path=["
			//		+ path.toString() + "], while DESC =[" + DESC_FILE + "].");
			if (path.getFileName().toString().equals(DESC_FILE)) {
				log.info("Find one desc file at [" + path.toString() + "].");
				result.add(new ResDescFile(path.getParent().toString(), path.getFileName().toString()));
			}
		}
	}

	@Override
	public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
		find(file);
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
		find(dir);
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult visitFileFailed(Path file, IOException e) {
		System.out.println(e);
		return FileVisitResult.CONTINUE;
	}

	public List<ResDescFile> getResult() {
		return result;
	}

	public void setResult(List<ResDescFile> result) {
		this.result = result;
	}

}
